package org.smallframework.spring.mapper;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.Assert;
import sf.database.dao.DBClient;

/**
 * 对工厂Bean的实现用于构建Mapper,一次只对一个接口进行扫描，构建Mapper
 * 要获取FactoryBean本身 需要加&
 * @param <T>
 */
public class DaoMapperFactoryBean<T> implements FactoryBean<T> {
    private Class<T> mapperInterface;
    private DBClient client;

    public DaoMapperFactoryBean(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public DaoMapperFactoryBean() {
    }

    protected void checkDaoConfig() throws IllegalArgumentException {
        Assert.notNull(this.mapperInterface, " 'mapperInterface' 属性是必须的");
    }

    @Override
    public T getObject() throws Exception {
        return this.client.getMapper(mapperInterface);
    }

    @Override
    public Class<T> getObjectType() {
        return this.mapperInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    //在扫描时会注入该属性
    public void setMapperInterface(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public void setClient(DBClient client) {
        this.client = client;
    }
}
